﻿
Imports std

Public Module nettransDataParser
    Private Function matchpair(ByVal data As nettransData) As Boolean
        assert(Not data Is Nothing)
        Return (data.size() And 1) = 0
    End Function

    Public Function keyvaluepair(ByVal data As nettransData, _
                                 ByRef pairs As vector(Of pair(Of String, String))) As Boolean
        assert(Not data Is Nothing, "data is nothing when parse nettransData")
        If Not matchpair(data) Then
            Return False
        End If
        If pairs Is Nothing Then
            pairs = New vector(Of pair(Of String, String))
        Else
            pairs.clear()
        End If

        Dim i As Int64
        For i = 0 To data.fieldCount() - 1 Step 2
            pairs.push_back(make_pair(data.take(Of String)(i), data.take(Of String)(i + 1)))
        Next

        Return True
    End Function

    Public Function keyvaluepair(ByVal data As nettransData, _
                                 ByRef pairs As map(Of String, String)) As Boolean
        assert(Not data Is Nothing, "data is nothing when parse nettransData")
        If Not matchpair(data) Then
            Return False
        End If
        If pairs Is Nothing Then
            pairs = New map(Of String, String)
        Else
            pairs.clear()
        End If

        Dim i As Int64
        For i = 0 To data.fieldCount() - 1 Step 2
            pairs(data.take(Of String)(i)) = data.take(Of String)(i + 1)
        Next

        Return True
    End Function

End Module
